<template>
  <div>
    <el-button type="text" icon="el-icon-circle-plus" size="mini" @click="openShop">添加</el-button>
    <!-- 弹框 -->
    <el-dialog
      v-dialogDrag
      ref="dialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="dialogVisible"
      :append-to-body="true"
      title="配件选择"
      width="70%"
      @close="close">
      <!-- 查询条件 -->
      <el-form ref="searchForm" :model="search" label-width="95px" label-position="right" size="mini">
        <el-row :gutter="6">
          <el-col :xs="24" :sm="8" :md="6">
            <el-form-item prop="dealerCode" label="订货单位">
              <r-select
                v-if="userInfo.dataType === 45081009"
                v-model="search.dealerCode"
                :disabled="purchaseDis"
                :clearable="true"
                placeholder="请选择"
                selectid="DEALER_CODE"
                selectname="DEALER_NAME"
                url="/part/common/queryPurchaseDealer"
                model="DMSCLOUD_PART"/>
              <el-input v-if="userInfo.dataType !== 45081009" v-model="search.dealerName" :disabled="purchaseDis"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="8" :md="6">
            <el-form-item prop="orderNo" label="订单编号">
              <el-input v-model="search.orderNo" clearable/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="8" :md="6">
            <el-form-item prop="orderType" label="订单类型">
              <r-select :include-code="includeCodeVal" :exclude-code="excludeCodeVal" v-model="search.orderType" type="6004" placeholder="请选择" clearable/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="8" :md="6">
            <el-form-item prop="shippingCondition" label="运输方式">
              <r-select v-model="search.shippingCondition" type="6028" placeholder="请选择" clearable/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="8" :md="6">
            <el-form-item prop="saleOrderNo" label="销售单号">
              <el-input v-model="search.saleOrderNo" clearable/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="8" :md="6">
            <el-form-item label="配货日期" prop="salesDate">
              <el-date-picker
                v-model="search.salesDate"
                :unlink-panels="true"
                type="daterange"
                value-format="yyyy-MM-dd"
                start-placeholder="开始日期"
                end-placeholder="结束日期"/>
            </el-form-item>
          </el-col>

          <div style="text-align:right">
            <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
            <el-button icon="el-icon-refresh" @click="resetForm">重置</el-button>
          </div>
        </el-row>
      </el-form>
      <el-table
        v-loading="load"
        ref="salesTable"
        :max-height="300"
        :data="orderList"
        stripe
        border
        @selection-change="select"
      >
        <el-table-column label="序号" width="50" type="index"/>
        <!--  <el-table-column label="操作" min-width="50">
          <template slot-scope="scope">
            <el-tooltip class="item" content="选择" placement="bottom">
              <el-button type="text" icon="el-icon-check" style="font-size: 20px;" @click="selectPart(scope.row)"/>
            </el-tooltip>
          </template>
        </el-table-column>-->
        <el-table-column type="selection" min-width="50"/>
        <el-table-column :show-overflow-tooltip="true" prop="SO_NO" label="销售单号" min-width="130"/>
        <el-table-column :show-overflow-tooltip="true" prop="ORDER_NO" label="订单编号" min-width="130"/>
        <el-table-column :formatter="dateFormat" prop="ORDER_DATE" label="订单日期" min-width="140"/>
        <el-table-column :formatter="dateFormat" prop="SALES_DATE" label="配货日期" min-width="140"/>
        <el-table-column :formatter="filterCode" prop="ORDER_TYPE" label="订单类型" min-width="80" type="6004"/>
        <el-table-column :formatter="filterCode" prop="OLD_ORDER_TYPE" label="原订单类型" min-width="100" type="6004"/>
        <el-table-column :formatter="filterCode" prop="SHIPPING_CONDITION" label="运输方式" min-width="80" type="6028"/>
        <el-table-column :formatter="formatCurrency" prop="SALES_SUM" label="金额" min-width="100"/>
        <el-table-column :show-overflow-tooltip="true" prop="RECV_ADDRESS" label="收货地址" min-width="120"/>
        <el-table-column :show-overflow-tooltip="true" prop="REMARK" label="备注" min-width="120"/>
      </el-table>
      <!-- 分页 -->
      <div style="text-align:right">
        <Pagination :data="page" @change-size="handleSizeChange" @change-page="handleCurrentChange"/>
      </div>
      <!--
        <el-table ref="partTable" :max-height="300" :data="partTableData" stripe border>
        <el-table-column label="序号" width="50" type="index"/>
        <el-table-column prop="PART_CODE" label="配件代码" min-width="130" show-overflow-tooltip/>
        <el-table-column prop="PART_NAME" label="配件名称" min-width="160" show-overflow-tooltip/>
        <el-table-column prop="UNIT" label="计量单位" min-width="80"/>
        <el-table-column prop="SALES_QUANTITY" label="数量" min-width="80"/>
        <el-table-column :formatter="formatCurrency" prop="UNIT_PRICE" label="单价" min-width="80"/>
        <el-table-column :formatter="formatCurrency" prop="ITEM_NETVALUE" label="金额" min-width="100"/>
      </el-table>
    -->
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="text">
          <i class="el-icon-search magnifier" />
        </el-button>
        <el-button type="primary" @click="req">确 定</el-button>
        <el-button @click="close">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryOrder, queryPartsBySaleId
} from '@/api/part/dcs/ordermanage/partSales'
import Pagination from '@/components/Pagination'
import formMixins from '@/mixins/formMixins'
import { mapGetters } from 'vuex'
import rSelect from '@/components/Select/Select'
import _ from 'lodash'

export default {
  name: 'PartSelect',
  components: {
    Pagination,
    rSelect
  },
  mixins: [formMixins],
  props: {
    orderFormObj: Object
  },
  data() {
    return {
      load: false,
      page: {},
      orderList: [],
      partTableData: [],
      multipleSelection: [],
      purchaseDis: false,
      includeCodeVal: '',
      excludeCodeVal: '', // 保外订单、保外油品
      search: {
        limit: 10,
        pageNum: 1,
        order: 'desc',
        sort: 'SALES_DATE',
        action: 'QUERY',
        dealerCode: '',
        dealerName: '',
        shippingCondition: '',
        saleOrderNo: '',
        orderNo: '',
        orderType: '',
        salesDate: []
      },
      dialogVisible: false
    }
  },
  computed: {
    ...mapGetters(['userInfo', 'systemParam'])
  },
  mounted() {
    if (this.userInfo.dataType && this.userInfo.dataType === 45081009) {
      this.excludeCodeVal = '60041005,60041006,60041009'
    } else if (this.userInfo.dataType && this.userInfo.dataType === 45081013) {
      this.includeCodeVal = '60041005,60041006,60041010'
    }
  },
  methods: {
    openShop() {
      if (this.orderFormObj != null) {
      /*  if (!this.orderFormObj.dealerCode) {
          this.$message({
            type: 'info',
            message: '请选择订货单位!'
          })
          return false
        }*/
        if (this.orderFormObj.dealerCode) {
          this.purchaseDis = true
          this.$set(this.search, 'dealerCode', this.orderFormObj.dealerCode)
          this.$set(this.search, 'dealerName', this.orderFormObj.dealerName)
        } else {
          this.purchaseDis = false
        }
        /*  if (this.orderFormObj.shippingCondition) {
          this.$set(this.search, 'shippingCondition', this.orderFormObj.shippingCondition)
        }*/

        this.dialogVisible = true
        this.searchHandle()
      }
    },
    searchHandle() {
      this.load = true
      const search = _.extend(this.search)
      if (this.search.salesDate != null && this.search.salesDate.length > 0) {
        search.saleDateStr = this.search.salesDate[0]
        search.saleDateEnd = this.search.salesDate[1]
      } else {
        search.saleDateStr = ''
        search.saleDateEnd = ''
      }

      this.partTableData = []
      queryOrder(search)
        .then(res => {
          this.orderList = res.rows
          this.page = res
          this.load = false
        })
        .catch(err => {
          this.load = false
        })
    },
    // 选择分页
    handleSizeChange(size) {
      this.search.limit = size
      this.searchHandle()
    },
    handleCurrentChange(page) {
      this.search.pageNum = page
      this.searchHandle()
    },

    selectPart(data) {
      const params = {
        saleOrderId: data.SO_ID
      }
      queryPartsBySaleId(params)
        .then(res => {
          this.partTableData = res
        })
        .catch(err => {
        })
    },

    handleSelectionChange(val) {
      this.multipleSelection = val
    },

    req() {
    /*  const val = this.partTableData
      if (!val || val.length === 0) {
        this.$message({
          type: 'info',
          message: '请选择数据!'
        })
        return false
      }
      this.$emit('getItemInfo', val)*/
      // 判断是不是不同家的
      if (this.multipleSelection.length === 0) {
        this.$message({ type: 'info', message: '请选择数据!' })
      } else {
        for (let i = 0; i < this.multipleSelection.length; i++) {
          for (let j = (i + 1); j < this.multipleSelection.length; j++) {
            if (this.multipleSelection[i].DEALER_CODE !== this.multipleSelection[j].DEALER_CODE) {
              this.$message({
                type: 'warning',
                message: '订货单位不同，不能合并发运!'
              })
              return
            }
          }
        }

        this.multipleSelection.forEach(item => {
          const params = {
            saleOrderId: item.SO_ID
          }
          queryPartsBySaleId(params)
            .then(res => {
              // this.partTableData = res
              this.$emit('getItemInfo', res)
            })
            .catch(err => {
            })
        })
      }
    },
    // 选中数据
    select(val) {
      if (val === null) {
        this.multipleSelection = []
      } else {
        this.multipleSelection = val
      }
    },
    // 重置
    resetForm() {
      this.$refs['searchForm'].resetFields()
      this.search.dealerCode = ''
      this.partTableData = []
    },

    // 取消
    close() {
      this.dialogVisible = false
      this.purchaseDis = false
      this.resetForm()
    }

  }
}

</script>

<style scoped>

</style>
